package com.imm.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.imm.common.Result;
import com.imm.mapper.EmployeeMapper;
import com.imm.pojo.Employee;
import com.imm.service.IEmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Service
@Slf4j
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {
    @Override
    public Result<Employee> login(Employee employee, HttpServletRequest request) {
        String password = employee.getPassword();
        // md5加密
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        // 查询数据库
        Employee emp = this.getOne(wrapper);
        // 用户不存在
        if (emp == null) {
            return Result.error("用户不存在");
        }
        // 密码错误
        if (!emp.getPassword().equals(password)) {
            return Result.error("密码错误");
        }
        // 员工状态
        if (emp.getStatus() == 0) {
            return Result.error("该账号被禁用");
        }
        request.getSession().setAttribute("employee", emp.getId());
        return Result.success(emp);
    }

    @Override
    public Result<String> logout(HttpServletRequest request) {
        // 删除session
        request.getSession().removeAttribute("employee");
        return Result.success("退出成功");
    }

    @Override
    public Result<String> saveEmployee(Employee employee, HttpServletRequest request) {
        // 判断用户是否存在
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername, employee.getUsername());
        Employee emp = getOne(wrapper);
        System.out.println(emp);
        if (emp != null) {
            return Result.error("用户已存在");
        }
        // 设置初始密码 | md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        // 保存到数据库
        save(employee);
        return Result.success("新增员工成功");
    }

    @Override
    public Result<Page> pageEmployee(int page, int pageSize, String name) {
        // 分页构造器
        Page pageInfo = new Page(page, pageSize);
        // 条件构造器
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper();
        wrapper.like(StringUtils.isNotEmpty(name), Employee::getName, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        // 执行查询
        page(pageInfo, wrapper);
        return Result.success(pageInfo);
    }

    @Override
    public Result<String> updateEmployee(Employee employee, HttpServletRequest request) {
        // 获取当前登录用户信息
        Long emp = (Long)request.getSession().getAttribute("employee");
        employee.setUpdateUser(emp);
        employee.setUpdateTime(LocalDateTime.now());
        log.info("{}", employee);
        updateById(employee);
        return Result.success("修改成功");
    }

    @Override
    public Result<Employee> getEmployee(Long id) {
        Employee emp = getById(id);
        return Result.success(emp);
    }

}
